import { getUserDetailApi, changePasswordApi } from './change-user-password.service';
import { getUserInfo } from '@/utils/auth';
import { alertMessage } from '@/utils/utils';
export default {
  name: 'change-user-password',
  components: {},
  computed: {
    showDialog: {
      get: function() {
        this.getUserDetail();
        return this.$store.state.app.showDialog
      },
      set: function() { }
    }
  },
  data() {
    return {
      file_url: process.env.VUE_APP_FILE_URL, // 头像url
      userId: getUserInfo().userId,
      editForm: {},
      title: '修改密码'
    }
  },
  mounted() {
    this.getUserDetail();
  },
  methods: {
    // 获取详情
    getUserDetail() {
      getUserDetailApi(this.userId).then(res => {
        this.editForm = res.data;
      })
    },

    // 修改密码
    save() {
      if (this.editForm.newPassword !== this.editForm.renewPassword) {
        alertMessage('两次密码输入不一致,请重新输入', 'warning');
        return
      }
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          const params = {
            objectId: this.userId,
            oldPassword: this.editForm.oldPassword,
            newPassword: this.editForm.newPassword
          }
          changePasswordApi(params).then((res) => {
            this.loading = false;
            if (res.code === '506') {
              alertMessage(res.message, 'error');
            } else {
              alertMessage(res.message);
              this.closeDialog();
            }
          }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
        } else {
          this.loading = false
        }
      });
    },

    // 关闭dialog时需要告诉父组件隐藏弹窗
    closeDialog() {
      this.$store.dispatch("app/showChangePassword", false);
    }
  }
}
